package com.zp.business.controller.infusion;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.zp.business.entity.infusion.bo.InfusionTypeBo;
import com.zp.business.entity.infusion.vo.InfusionTypeVo;
import com.zp.business.service.infusion.IInfusionTypeService;
import com.zp.common.core.controller.BaseController;
import com.zp.common.core.entity.R;
import com.zp.common.core.validate.AddGroup;
import com.zp.common.core.validate.EditGroup;
import com.zp.common.excel.utils.ExcelUtil;
import com.zp.common.log.annotation.Log;
import com.zp.common.log.enums.BusinessType;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 输液方式配置控制器
 * 前端访问路由地址为:/business/infusionType
 *
 * @author zhang peng
 * @ date 2025-02-01
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/infusionType")
public class InfusionTypeController extends BaseController {

    private final IInfusionTypeService iInfusionTypeService;

    /**
     * 查询输液方式配置列表 (分页)
     */
    @SaCheckPermission("business:infusionType:list")
    @GetMapping("/selectPage")
    public TableDataInfo<InfusionTypeVo> selectPage(InfusionTypeBo bo, PageQuery pageQuery) {
        return iInfusionTypeService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询输液方式配置列表 (不分页)
     */
    @SaCheckPermission("business:infusionType:list")
    @GetMapping("/list")
    public R<List<InfusionTypeVo>> list(InfusionTypeBo bo) {
        List<InfusionTypeVo> list = iInfusionTypeService.queryList(bo);
        return R.ok(list);
    }

    /**
     * 导出输液方式配置列表
     */
    @SaCheckPermission("business:infusionType:export")
    @Log(title = "输液方式配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(InfusionTypeBo bo, HttpServletResponse response) {
        List<InfusionTypeVo> list = iInfusionTypeService.queryList(bo);
        ExcelUtil.exportExcel(list, "输液方式配置", InfusionTypeVo.class, response);
    }

    /**
     * 获取输液方式配置详细信息
     *
     * @param infusionTypeId 主键
     */
    @SaCheckPermission("business:infusionType:list")
    @GetMapping("/{infusionTypeId}")
    public R<InfusionTypeVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable String infusionTypeId) {
        return R.ok(iInfusionTypeService.queryById(infusionTypeId));
    }

    /**
     * 新增输液方式配置
     */
    @SaCheckPermission("business:infusionType:add")
    @Log(title = "输液方式配置", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody InfusionTypeBo bo) {
        return toAjax(iInfusionTypeService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改输液方式配置
     */
    @SaCheckPermission("business:infusionType:edit")
    @Log(title = "输液方式配置", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody InfusionTypeBo bo) {
        return toAjax(iInfusionTypeService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除输液方式配置
     *
     * @param infusionTypeIds 主键串
     */
    @SaCheckPermission("business:infusionType:remove")
    @Log(title = "输液方式配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{infusionTypeIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable String[] infusionTypeIds) {
        return toAjax(iInfusionTypeService.deleteWithValidByIds(Arrays.asList(infusionTypeIds), true) ? 1 : 0);
    }
}
